import org.apache.commons.lang.builder.EqualsBuilder
import org.apache.commons.lang.builder.HashCodeBuilder

class Book {

	String title

	String author

	String bookCode

	boolean canRent = true

    	static hasMany = [ rents : Rent ]

	static constraints = {
		title(blank: false, unique: true)
		author(blank: false)
		bookCode(blank: true)
	}

	String toString() {
		return title	
	}

	def boolean equals(obj) {
     		if (!(obj instanceof Book)) {
         		return false
      		}
      		Book other = (Book) obj
         	return new EqualsBuilder().
             	append(this.title, other.title). 	    
             	isEquals()
  	}

	def int hashCode() {
      		return new HashCodeBuilder(17, 37).  
	   	append(this.title).
	   	toHashCode()
	}

	static List listBooksAvailableToRent() {
		def books = Book.findAllByCanRent(true);
		Set set = new HashSet(books);
		def rents = Rent.findAllRented();
		if(rents.size == 0) {
			return books;
		} else {
			for (i in rents){ 
				def book = i.getBook();
				if(set.contains(book)) {
					set.remove(book);
				}
			}
		def list = new ArrayList(set)
		return list;
		}

	}
}
